﻿#include "ParseUtil.h"

#include "proto_common_base.nanopb.h"
#include "proto_common_map.nanopb.h"
#include "NFComm/NFPluginModule/NFCheck.h"

int CParseUtil::SplitCharArray(const char* pString, const char* delim, std::vector<std::string>& vecStr)
{
    if (!pString)
    {
        return -1;
    }

    char localString[2048];
    strncpy(localString, pString, sizeof(localString));
    char* pResult = strtok(localString, delim);

    while (pResult)
    {
        std::string tmpStr = pResult;

        vecStr.push_back(tmpStr);
        pResult = strtok(NULL, delim);
    }

    return 0;
}

int CParseUtil::ParseStringToIntArray(const char* pString, const char* delim, int intArray[], int iMaxSize, int& iSize)
{
    if (!pString)
    {
        return -1;
    }

    char localString[1024];
    strncpy(localString, pString, sizeof(localString));
    int i = 0;
    char* pResult = strtok(localString, delim);
    while (pResult)
    {
        if (i >= iMaxSize)
        {
            iSize = i;
            return -1;
        }
        intArray[i] = atoi(pResult);
        pResult = strtok(NULL, delim);
        i++;
    }

    iSize = i;

    return 0;
}

int CParseUtil::ParseStringToFloatArray(const char* pString, const char* delim, float fArray[], int iMaxSize, int& iSize)
{
    if (!pString)
    {
        return -1;
    }

    char localString[1024];
    strncpy(localString, pString, sizeof(localString));
    int i = 0;
    char* pResult = strtok(localString, delim);
    while (pResult)
    {
        if (i >= iMaxSize)
        {
            iSize = i;
            return -1;
        }
        fArray[i] = atof(pResult);
        pResult = strtok(NULL, delim);
        i++;
    }

    iSize = i;

    return 0;
}

int CParseUtil::ParseIntArrayArray(IntArrayArray* pArrArr, const char* pString)
{
    CHECK_NULL(0, pArrArr);
    CHECK_NULL(0, pString);

    char sBuff[1024] = {0};
    strncpy(sBuff, pString, sizeof(sBuff));

    char* pStart = sBuff;

    pArrArr->array_count = 0;
    while (pStart && pStart[0] != 0)
    {
        char* pValue = pStart;
        char* pNext = strstr(pStart, "|");
        if (pNext)
        {
            *pNext = 0;
            pStart = pNext + 1;
        }
        else
        {
            pStart = NULL;
        }

        IntArray* pArr = pArrArr->array + pArrArr->array_count++;
        pArr->value_count = 0;

        while (pValue && pValue[0] != 0)
        {
            char* p2Value = pValue;
            char* p2Next = strstr(pValue, ",");
            if (p2Next)
            {
                *p2Next = 0;
                pValue = p2Next + 1;
            }
            else
            {
                pValue = NULL;
            }

            pArr->value[pArr->value_count++] = atoi(p2Value);
        }
    }

    return 0;
}

int CParseUtil::ParseOneMacro(AIMacro* pMacro, const char* pString)
{
    CHECK_NULL(0, pMacro);
    CHECK_NULL(0, pString);

    char sBuff[1024] = {0};
    strncpy(sBuff, pString, sizeof(sBuff));

    char* pStart = sBuff;
    if (sBuff[0] == '|')
    {
        pStart++;
        pMacro->type = 1;
    }
    else
    {
        pMacro->type = 0;
    }

    pMacro->sets_count = 0;
    while (pStart && pStart[0] != 0)
    {
        char* pValue = pStart;
        char* pNext = strstr(pStart, "|");
        if (pNext)
        {
            *pNext = 0;
            pStart = pNext + 1;
        }
        else
        {
            pStart = NULL;
        }

        MacroSections* pMacroSets = pMacro->sets + pMacro->sets_count++;
        pMacroSets->set_count = 0;

        while (pValue && pValue[0] != 0)
        {
            char* p2Value = pValue;
            char* p2Next = strstr(pValue, ",");
            if (p2Next)
            {
                *p2Next = 0;
                pValue = p2Next + 1;
            }
            else
            {
                pValue = NULL;
            }

            MacroSection* pMacroSet = pMacroSets->set + pMacroSets->set_count++;
            pMacroSet->skill_id_count = 0;

            while (p2Value && p2Value[0] != 0)
            {
                char* p3Value = p2Value;
                char* p3Next = strstr(p2Value, "#");
                if (p3Next)
                {
                    *p3Next = 0;
                    p2Value = p3Next + 1;
                }
                else
                {
                    p2Value = NULL;
                }

                int iSkillID = atol(p3Value);

                pMacroSet->skill_id[pMacroSet->skill_id_count++] = iSkillID;
            }
        }
    }

    return 0;
}

int CParseUtil::ParseMacroToVector(const AIMacro* pMacro, std::vector<std::vector<std::vector<int>>>& vvvecSkillID)
{
    CHECK_NULL(0, pMacro);

    vvvecSkillID.clear();
    for (int i = 0; i < pMacro->sets_count && i < (int)ARRAYSIZE(pMacro->sets); ++i)
    {
        std::vector<std::vector<int>> vvecSkillID;
        vvecSkillID.clear();

        for (int j = 0; j < pMacro->sets[i].set_count && j < (int)ARRAYSIZE(pMacro->sets[i].set); ++j)
        {
            std::vector<int> vecSkillID;
            vecSkillID.clear();

            for (int k = 0; k < pMacro->sets[i].set[j].skill_id_count && k < (int)ARRAYSIZE(pMacro->sets[i].set[j].skill_id); ++k)
            {
                vecSkillID.push_back(pMacro->sets[i].set[j].skill_id[k]);
            }

            if (vecSkillID.size() > 0)
            {
                vvecSkillID.push_back(vecSkillID);
            }
        }

        if (vvecSkillID.size() > 0)
        {
            vvvecSkillID.push_back(vvecSkillID);
        }
    }

    return 0;
}


